package com.spring_mvc.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import java.beans.PropertyVetoException;

@PropertySource("classpath:jdbc.properties")
public class DataSource {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;


    @Bean("dataSource")  //Spring会将当前方法的返回值以指定名称存储到Spring容器中
    public DruidDataSource getDataSource() throws PropertyVetoException {
        DruidDataSource data = new DruidDataSource();
        data.setDriverClassName(driver);
        data.setUrl(url);
        data.setUsername(username);
        data.setPassword(password);
        return data;
    }

}
